
#include<bits/stdc++.h>
#define gc getchar
#define pc putchar
using namespace std;
int read(){
    int x=0,f=1;
    char ch=gc();
    while(!isdigit(ch) && ch!='-')ch=gc();
    if(ch=='-'){
        f=-1;
        ch=gc();
    }
    while(isdigit(ch)){
        x=(x<<3)+(x<<1)+(ch^48);
        ch=gc();
    }
    return x*f;
}
void print(int x){
    if(x<0){
        pc('-');
        x=-x;
    }
    if(x<10){
        pc(x^48);
        return;
    }
    print(x/10);
    pc((x%10)^48);
}
int n,m;
int YS[100100];//genshin
int a[100100];
int c[100100];
bool chk[30][70000];
int winner[30][110000];

bool lg(){
    char ch=gc();
    while(ch!='0' && ch!='1')ch=gc();
    return (ch=='1');
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++)YS[i]=read();
    for(int i=1;i<=m;i++)c[i]=read();
    int NN=ceil(log(n)/log(2));
    //cout<<NN<<endl;
    for(int i=1;i<=NN;i++){
        int TT=(1<<NN-i);
        for(int j=1;j<=TT;j++)chk[i][j]=lg();
    }
    //cout<<"chk"<<endl;
    int T=read();
    while(T--){
        int x[5];
        x[0]=read(),x[1]=read(),x[2]=read(),x[3]=read();
        for(int i=1;i<=n;i++)a[i]=YS[i]^x[i%4];
        int ret=0;
        for(int TTTT=1;TTTT<=m;TTTT++){
            int loger=log(c[TTTT])/log(2);
            for(int i=1;i<=c[TTTT];i++)winner[0][i]=i;
            if(c[TTTT]==1){
                ret^=(TTTT);
                continue;
            }
            for(int R=1;R<=loger;R++){
                int TTTTT=(1<<loger-R);
                for(int j=1;j<=TTTTT;j++){
                    int lft=winner[R-1][j*2-1],rgt=winner[R-1][j*2];
                    if(chk[R][j]){
                        if(a[rgt]>R)winner[R][j]=rgt;
                        else winner[R][j]=lft;
                    }else{
                        if(a[lft]>=R)winner[R][j]=lft;
                        else winner[R][j]=rgt;
                    }
                }
            }
            ret^=(TTTT*winner[loger][1]);
        }
        print(ret),pc('\n');
    }
}
/*
STO TheSky233 Orz
STO LXGOD Orz
STO rym Orz
STO czx Orz
(can't remember rym and czx's id)
rp+=inf;
luogu_uid=1038710
STO give me 1= please Orz
STO give me 1= please Orz
STO give me 1= please Orz
STO give me 1= please Orz
STO give me 1= please Orz
STO Let me get to NOIP Orz
STO Let me get to NOIP Orz
STO Let me get to NOIP Orz
STO Let me get to NOIP Orz
STO Let me get to NOIP Orz
Genshin , shutdown(i don't play it)
C++ , start!!!
*/
